Ext.define('YX.core.util.ReportWindow', {
	extend : 'Ext.window.Window',
	//alternateClassName : ['ReportWindow', 'Report'],
	//alias: 'widget.reportWindow',
	frame : false,
	frameHeader : true,
	//singleton : true,
	maximizable : true,
	maximized : true,
	closeAction : 'destroy',
	modal : true,
	layout : 'fit',
	pelRpt : null,
	rpt : null,
	report : null,
	initComponent : function() {
		var rptReport = this.rpt;
		if (!Ext.String.endsWith(rptReport, '.grf', true)) {
			//rptReport = rptReport.substr(0, rptReport.length - 4);
			rptReport = rptReport + ".grf";
		}
		if (rptReport.indexOf("/") < 0) {
			//rpt = appFolder + "/rpt/template/" + rpt;
			rptReport = "files/template/" + rptReport;
		}

		var typeid;
		if (Ext.isIE)
			typeid = 'classid="clsid:ABB64AAC-D7E8-4733-B052-1B141C92F3CE" '
			  + gr_CodeBase;
		else
			typeid = 'type="application/x-grplugin6-printviewer"';
		var rptObj = '<object id="_gppreport" '
		  + typeid
		  + ' width="100%" height="100%"><param name="AutoRun" value="false"><param name="SerialNo" value="'
		  + gr_SerialNo + '">' + '<param name="ReportURL" value="' + rptReport + '">'
		  + '<param name="UserName" value="' + gr_UserName + '"></object>';
		/*
		if (Ext.isIE)
			typeid = 'classid="clsid:396841CC-FC0F-4989-8182-EBA06AA8CA2F" ';
		else
			typeid = 'type="application/x-grplugin6-report" ';
		typeid += gr_CodeBase;
		var rptObj = '<object id="Report" ' + typeid
		  + ' width="0" height="0" style="display:block;margin-top:-16;" VIEWASTEXT></object>';
		  */

		/*
		this.pelRpt = Ext.create('Ext.panel.Panel', {
		 html : rptObj
		});*/
		this.pelRpt = Ext.create('Ext.ux.IFrame');

		this.items = [this.pelRpt];

		this.on('beforeshow', this.onBeforeShow, this);
		this.callParent();

		//Report.Register(gr_UserName, gr_SerialNo);
		this.on('afterrender', this.onAfterRender, this);
	},
	onAfterRender : function() {
		var rptReport = this.rpt;
		if (!Ext.String.endsWith(rptReport, '.grf', true)) {
			//rptReport = rptReport.substr(0, rptReport.length - 4);
			rptReport = rptReport + ".grf";
		}
		if (rptReport.indexOf("/") < 0) {
			//rpt = appFolder + "/rpt/template/" + rpt;
			rptReport = "files/template/" + rptReport;
		}

		var typeid;
		if (Ext.isIE)
			typeid = 'classid="clsid:ABB64AAC-D7E8-4733-B052-1B141C92F3CE" '
			  + gr_CodeBase;
		else
			typeid = 'type="application/x-grplugin6-printviewer"';
		var rptObj = '<object id="_gppreport" '
		  + typeid
		  + ' width="100%" height="100%"><param name="AutoRun" value=false><param name="SerialNo" value="'
		  + gr_SerialNo + '">' + '<param name="ReportURL" value="' + rptReport + '">'
		  + '<param name="UserName" value="' + gr_UserName + '"></object>';
		var el = this.pelRpt.getEl();
		el.query('iframe')[0].contentDocument.write(rptObj);
	},
	onBeforeShow : function() {
	},
	loadReport : function(rptName) {
		var rptReport = rptName;
		if (!Ext.String.endsWith(rptReport, '.grf', true)) {
			//rptReport = rptReport.substr(0, rptReport.length - 4);
			rptReport = rptReport + ".grf";
		}
		if (rptReport.indexOf("/") < 0) {
			//rpt = appFolder + "/rpt/template/" + rpt;
			rptReport = "files/template/" + rptReport;
		}

		var typeid;
		if (Ext.isIE)
			typeid = 'classid="clsid:ABB64AAC-D7E8-4733-B052-1B141C92F3CE" '
			  + gr_CodeBase;
		else
			typeid = 'type="application/x-grplugin6-printviewer"';
		var rptObj = '<object id="_gppreport" '
		  + typeid
		  + ' width="100px" height="100px"><param name="AutoRun" value="false"><param name="SerialNo" value="'
		  + gr_SerialNo + '">' //+ '<param name="ReportURL" value="' + rptReport + '">'
		  + '<param name="UserName" value="' + gr_UserName + '"></object>';
		var el = this.pelRpt.getEl();
		el.query('iframe')[0].contentDocument.write(rptObj);
	},
	/*
	show : function(rptName, data) {
	if (Ext.isEmpty(rptName)) {
	var me = this;
	new Ext.util.DelayedTask(function() {
		  me.close();
	  }).delay(500);
	return;
	}

	this.hidden = true;
	this.callParent();

	var appFolder = Ext.app.Application.instance.appFolder;
	if (!Ext.String.endsWith(rptName.toLowerCase(), '.grf', true)) {
	rptName += '.grf';
	}

	this.rptUrl = "/resources/template/" + rptName;
	console.log(this.getReport());
	//this.report = this.getReport().Report;

	//this.report = Ext.getDom('_gppreport');
	if (!this.rptUrl) {
	//CreatePrintViewerEx2('_gppreport', "100%", "100%", "rpt/" + rptName, "", false, "");

	var typeid;
	if (Ext.isIE)
		typeid = 'classid="clsid:ABB64AAC-D7E8-4733-B052-1B141C92F3CE" '
		  + gr_CodeBase;
	else
		typeid = 'type="application/x-grplugin6-printviewer"';

	this.pelRpt
	  .update('<object id="_gppreport" '
	    + typeid
	    + ' width="100%" height="100%"><param name="AutoRun" value="false"><param name="SerialNo" value="'
	    + gr_SerialNo + '"><param name="UserName" value="' + gr_UserName
	    + '"></object>');
	} else {
	var rpt = this.getReport();
	//rpt.Report.loadFromURL(this.rptUrl);
	//rpt.start();
	}

	if (data) {
	this.data = data;
	//report.loadDataFromXml(data);
	//report.run();
	}
	//this.maximize();
	//this.center();
	},*/
	onShow : function() {
		this.callParent(arguments);

		//this.maximize();
		//this.center();
		if (this.report) {
			this.report.Start();
			this.report.PrintPreview();
		}
	},
	onHide : function() {
		this.callParent(arguments);
		if (this.report) {
			this.report.Stop();
		}
	},
	getReport : function() {
		//return _gppreport;
		return this.pelRpt.getEl().query('iframe')[0].contentWindow._gppreport;
	}
});